/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.foundation.poi;

import com.google.common.collect.Maps;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;

import java.util.Map;

/**
 * Created by cherry on 2016/6/3.
 */
public class CellStyleUtil {

    public static Map<String, XSSFCellStyle> getStyle(XSSFWorkbook workbook) {
        Map<String, XSSFCellStyle> resultMap = Maps.newHashMap();
        Map<String, Short> styleParam = Maps.newHashMap();

        if (resultMap.size() == 0) {
            styleParam.put("foreGroundColor", IndexedColors.GREY_25_PERCENT.index);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.summary_title.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.summary_content.toString(), getStyle(workbook, styleParam));
            resultMap.put(CellStyleType.testItems_tbSn_data.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.GREEN.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.summary_content_Excellent.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.SKY_BLUE.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.summary_content_Adequate.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.summary_content_Marginal.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.RED.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.summary_content_Bad.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("alignment", XSSFCellStyle.ALIGN_RIGHT);
            resultMap.put(CellStyleType.summary_params_text.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("fontColor", IndexedColors.GREEN.index);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_LEFT);
            resultMap.put(CellStyleType.summary_params_Excellent.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("fontColor", IndexedColors.SKY_BLUE.index);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_LEFT);
            resultMap.put(CellStyleType.summary_params_Adequate.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("fontColor", IndexedColors.YELLOW.index);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_LEFT);
            resultMap.put(CellStyleType.summary_params_Marginal.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("fontColor", IndexedColors.RED.index);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_LEFT);
            resultMap.put(CellStyleType.summary_params_Bad.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.testItems_param_l_1.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.GREY_25_PERCENT.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.testItems_param_l_2.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.GREY_25_PERCENT.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("fontColor", IndexedColors.RED.index);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.testItems_param_l_3.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_RIGHT);
            resultMap.put(CellStyleType.testItems_param_m_1.toString(), getStyle(workbook, styleParam));
            resultMap.put(CellStyleType.testItems_param_r_1.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.GREY_25_PERCENT.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_RIGHT);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.testItems_param_m_2.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("alignment", XSSFCellStyle.ALIGN_RIGHT);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.testItems_param_r_2.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.LIGHT_YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            styleParam.put("doubleDown", XSSFCellStyle.BORDER_DOUBLE);
            resultMap.put(CellStyleType.testItems_tbSn_title.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.LIGHT_YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.testItems_tbSn_rangeData.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.LIGHT_YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            styleParam.put("doubleDown", XSSFCellStyle.BORDER_DOUBLE);
            resultMap.put(CellStyleType.testItems_tbSn_avgData.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.GREY_25_PERCENT.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("borderType2o_lr", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.testItems_tbSn_operator.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.LIGHT_YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("borderType2o_lr", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.testItems_tbSn_trial.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.LIGHT_YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.testItems_tbSn_rangeName.toString(), getStyle(workbook, styleParam));
            resultMap.put(CellStyleType.testItems_tbSn_totalName.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.LIGHT_YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            styleParam.put("doubleDown", XSSFCellStyle.BORDER_DOUBLE);
            resultMap.put(CellStyleType.testItems_tbSn_avgName.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.LIGHT_YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("borderType3_l", XSSFCellStyle.BORDER_THICK);
            resultMap.put(CellStyleType.testItems_tbAnova_title_horizonal_l.toString(), getStyle(workbook, styleParam));
            resultMap.put(CellStyleType.testItems_tbSource_title_horizonal_l.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.LIGHT_YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("borderType2", XSSFCellStyle.BORDER_THICK);
            resultMap.put(CellStyleType.testItems_tbAnova_title_horizonal_m.toString(), getStyle(workbook, styleParam));
            resultMap.put(CellStyleType.testItems_tbSource_title_horizonal_m.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.LIGHT_YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("borderType3_r", XSSFCellStyle.BORDER_THICK);
            resultMap.put(CellStyleType.testItems_tbAnova_title_horizonal_r.toString(), getStyle(workbook, styleParam));
            resultMap.put(CellStyleType.testItems_tbSource_title_horizonal_r.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.LIGHT_YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.testItems_tbAnova_title_vertical.toString(), getStyle(workbook, styleParam));
            resultMap.put(CellStyleType.testItems_tbSource_title_vertical.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.LIGHT_YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.testItems_tbAnova_content.toString(), getStyle(workbook, styleParam));
            resultMap.put(CellStyleType.testItems_tbSource_content.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.LIGHT_YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_RIGHT);
            styleParam.put("borderType3_l", XSSFCellStyle.BORDER_THICK);
            resultMap.put(CellStyleType.testItems_tbSource_params_name.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.LIGHT_YELLOW.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("fontWeight", XSSFFont.BOLDWEIGHT_BOLD);
            styleParam.put("fontColor", IndexedColors.BLUE.index);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType3_r", XSSFCellStyle.BORDER_THICK);
            resultMap.put(CellStyleType.testItems_tbSource_params_value.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.ROSE.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("alignment", XSSFCellStyle.ALIGN_CENTER);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.testItems_tbSource_categoriesValue.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("foreGroundColor", IndexedColors.GREY_25_PERCENT.index);
            styleParam.put("pattern", CellStyle.SOLID_FOREGROUND);
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.items_title.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("borderType4", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.items_content.toString(), getStyle(workbook, styleParam));

            styleParam.clear();
            styleParam.put("singleUp", XSSFCellStyle.BORDER_THIN);
            resultMap.put(CellStyleType.testItems_table_up.toString(), getStyle(workbook, styleParam));
            }
        return resultMap;
    }

    private static XSSFCellStyle getStyle (XSSFWorkbook workbook, Map<String, Short> styleParam) {

        XSSFCellStyle style = workbook.createCellStyle();
        XSSFFont font = workbook.createFont();

        if (styleParam.containsKey("foreGroundColor")) {
            style.setFillForegroundColor(styleParam.get("foreGroundColor"));
        }

        if (styleParam.containsKey("pattern")) {
            style.setFillPattern(styleParam.get("pattern"));
        }

        if (styleParam.containsKey("fontWeight")) {
            font.setBoldweight(styleParam.get("fontWeight"));
        }

        if (styleParam.containsKey("fontColor")) {
            font.setColor(styleParam.get("fontColor"));
        }

        if (styleParam.containsKey("alignment")) {
            style.setAlignment(styleParam.get("alignment"));
        }

        if (styleParam.containsKey("borderType4")) {
            style.setBorderBottom(styleParam.get("borderType4")); //下边框
            style.setBorderLeft(styleParam.get("borderType4"));//左边框
            style.setBorderTop(styleParam.get("borderType4"));//上边框
            style.setBorderRight(styleParam.get("borderType4"));//右边框
        }

        if (styleParam.containsKey("borderType3_l")) {
            style.setBorderBottom(styleParam.get("borderType3_l")); //下边框
            style.setBorderLeft(styleParam.get("borderType3_l"));//左边框
            style.setBorderTop(styleParam.get("borderType3_l"));//上边框
            style.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
        }

        if (styleParam.containsKey("borderType3_r")) {
            style.setBorderBottom(styleParam.get("borderType3_r")); //下边框
            style.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
            style.setBorderTop(styleParam.get("borderType3_r"));//上边框
            style.setBorderRight(styleParam.get("borderType3_r"));//右边框
        }

        if (styleParam.containsKey("borderType2")) {
            style.setBorderBottom(styleParam.get("borderType2")); //下边框
            style.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
            style.setBorderTop(styleParam.get("borderType2"));//上边框
            style.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
        }

        if (styleParam.containsKey("borderType2o_lr")) {
            style.setBorderLeft(styleParam.get("borderType2o_lr"));//左边框
            style.setBorderRight(styleParam.get("borderType2o_lr"));//右边框
        }

        if (styleParam.containsKey("doubleDown")) {
            style.setBorderBottom(styleParam.get("doubleDown")); //双线下边框
        }


        if (styleParam.containsKey("singleUp")) {
            style.setBorderBottom(styleParam.get("singleUp")); //双线下边框
        }

        style.setFont(font);
        return style;
    }
}
